package com.qin.test;

import java.util.List;
import java.util.Map;

public class Reorder {

    //待排序list
    List list;
    //待排序map
    Map map;

    public Reorder(List list) {
        this.list = list;
    }

    public Reorder(Map map) {
        this.map = map;
    }


    public void reorderList(String id, String function_id, int old_order, int new_order){

        System.out.println("排序前：" + list.toString());
        String temp = (String) list.get(old_order);
        //往后移动（old_order到new_order之间的值往前移动一位）
        if(new_order > old_order){
            for(int i=old_order; i<new_order; i++){
                list.set(i, list.get(i+1));
            }
            list.set(new_order, temp);
        }
        //往前移动（new_order到old_order之间的值往后移动一位）
        else if(new_order < old_order){
            for(int i=new_order+1; i<old_order; i++){
                list.set(i, list.get(i-1));
            }
            list.set(new_order, temp);
        }

        System.out.println("排序后：" + list.toString());
    }

    public void reorderMap(String id, String function_id, int old_order, int new_order){

        System.out.println("排序前：" + map.toString());

        String temp = (String) map.get(old_order);
        //往后移动（old_order到new_order之间的值往前移动一位）
        if(new_order > old_order){
            for(int i=old_order; i<new_order; i++){
                map.put(i, map.get(i+1));
            }
            map.put(new_order, temp);
        }
        //往前移动（new_order到old_order之间的值往后移动一位）
        else if(new_order < old_order){
            for(int i=old_order; i>new_order; i--){
                map.put(i, map.get(i-1));
            }
            map.put(new_order, temp);
        }

        System.out.println("排序后：" + map.toString());
    }
}
